package android.slc.appbase.ui.vd;

import android.slc.adapter.LoadMoreDataFormat3;
import android.slc.adapter.PageModel;
import android.slc.appbase.api.slc.callback.SlcObserver;
import android.slc.appbase.ui.vd.wrapper.BaseListPageWrapper;
import android.slc.appbase.ui.vd.wrapper.IBaseListPageWrapper;

import java.io.Serializable;

/**
 * 分页查询基础类
 *
 * @param <T>
 */
public abstract class BaseListPageVmBox<T extends Serializable> extends BaseListCommonVmBox<T> implements IBaseListPageWrapper<T> {
    protected final BaseListPageWrapper<T> listPageWrapper;

    public BaseListPageVmBox() {
        this.listPageWrapper = createListWrapper();
    }

    protected BaseListPageWrapper<T> createListWrapper() {
        return new BaseListPageWrapper<T>(this) {

            @Override
            public void onLoadMore() {
                BaseListPageVmBox.this.loadMore();
            }
        };
    }

    public void refresh() {
        this.listPageWrapper.refresh();
    }

    @Override
    public final void loadMore() {
        IBaseListPageWrapper.super.loadMore();
    }

    /**
     * 相应成功
     *
     * @param data
     */
    @Override
    public void onSucceed(PageModel<T> data) {
        this.getLoadMoreDataFormat3().formatSimple(getDataList(), data);
        this.dataListOf.notifyChange();
    }

    @Override
    public final void onFailed(int errorCode, String errorMessage) {
        super.onFailed(errorCode, errorMessage);
    }

    @Override
    public void onError(int errorCode, String errorMessage) {
        onFailed(errorCode, errorMessage);
        this.getLoadMoreDataFormat3().loadMoreFail();
    }

    @Override
    public SlcObserver<PageModel<T>> getDataListSlcObserver() {
        return this.listPageWrapper.getDataListSlcObserver();
    }

    @Override
    public LoadMoreDataFormat3 getLoadMoreDataFormat3() {
        return this.listPageWrapper.getLoadMoreDataFormat3();
    }
}
